package com.shz.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.CreateBucketRequest;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;


public class OSSUtil {
private final static String ENDPOINT = "oss-cn-hongkong.aliyuncs.com";
private final static String ACCESS_KEY_ID = "LTAI5tPzabw9rULKkVQE5yjP";
private final static String ACCESS_KEY_SECRET = "aP3fyxtxmrTm1NRfXB1h3VccfGtrCj";
private final static String BUCKET_NAME = "dangjianhk";
public final static String SUFFER_URL = "dangjianhk.oss-cn-hongkong.aliyuncs.com";

    private final static String PARENT_DIR = "imgs";


    /**
     * 获取OSS对象
     *
     * @return 返回OSS对象
     */
    public static OSS getOSSClient() {
        OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        // 验证仓库是否存在
        if (!ossClient.doesBucketExist(BUCKET_NAME)) {
            CreateBucketRequest createBucketRequest = new CreateBucketRequest(BUCKET_NAME);
            // 设置成可读写的
            createBucketRequest.setCannedACL(CannedAccessControlList.PublicReadWrite);
            // 创建
            ossClient.createBucket(createBucketRequest);
        }
        return ossClient;
    }

    /**
     * 删除图片文件
     *
     * @param filePath
     * @return
     */
    public static boolean deleteFile(String filePath) {
        OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        ossClient.deleteObject(BUCKET_NAME, filePath);
        ossClient.shutdown();
        return true;
    }

    /**
     * 上传图片文件
     *
     * @param multipartFile 文件
     * @return 返回图片上传路径
     */
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

    public static String uploadImg(MultipartFile multipartFile) {
        String filename = multipartFile.getOriginalFilename();
        // 获取文件后缀
        int x = filename.lastIndexOf(".");
        String etx = "";
        if (x != -1) {
            etx = filename.substring(x);
        }
        //一个文件夹的最大容量为Integer.MAX_VALUE就是2的32次方-1个,所以按日期分文件夹比较好
        //上传的文件路径
        String filePath = PARENT_DIR + "/shz/" + sdf.format(new Date()) + "/" + UUID.randomUUID().toString().replace("-", "") + etx;
        OSS ossClient = getOSSClient();


        String imgPath = "";
        try {

            imgPath = SUFFER_URL + filePath;
            ossClient.putObject(BUCKET_NAME, filePath, new ByteArrayInputStream(multipartFile.getBytes()));

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭OSS对象
            ossClient.shutdown();
        }
        return filePath;
    }


}
